package routes

import (
	"rbac-system/controllers"
	"rbac-system/middleware"

	"github.com/gin-gonic/gin"
)

func SetupRoutes(router *gin.Engine) {
	// 认证路由
	auth := router.Group("/api/auth")
	{
		auth.POST("/login", controllers.Login)
		auth.POST("/register", controllers.Register)
	}

	// 需要认证的路由
	api := router.Group("/api")
	api.Use(middleware.AuthMiddleware())
	{
		// 用户管理
		users := api.Group("/users")
		{
			users.GET("", controllers.GetUsers)
			users.GET("/:id", controllers.GetUser)
			users.POST("", controllers.CreateUser)
			users.PUT("/:id", controllers.UpdateUser)
			users.DELETE("/:id", controllers.DeleteUser)
			users.PUT("/:id/password", controllers.ChangePassword)
		}

		// 角色管理
		roles := api.Group("/roles")
		{
			roles.GET("", controllers.GetRoles)
			roles.GET("/:id", controllers.GetRole)
			roles.POST("", controllers.CreateRole)
			roles.PUT("/:id", controllers.UpdateRole)
			roles.DELETE("/:id", controllers.DeleteRole)
			roles.PUT("/:id/permissions", controllers.UpdateRolePermissions)
		}

		// 权限管理
		permissions := api.Group("/permissions")
		{
			permissions.GET("", controllers.GetPermissions)
			permissions.GET("/:id", controllers.GetPermission)
			permissions.POST("", controllers.CreatePermission)
			permissions.PUT("/:id", controllers.UpdatePermission)
			permissions.DELETE("/:id", controllers.DeletePermission)
			permissions.GET("/tree", controllers.GetPermissionTree)
		}

		// 个人中心
		profile := api.Group("/profile")
		{
			profile.GET("", func(c *gin.Context) {
				user, _ := c.Get("user")
				c.JSON(200, gin.H{"user": user})
			})
		}
	}

	// 健康检查
	router.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{"status": "ok", "message": "RBAC系统运行正常"})
	})
}
